const Router = require('koa-router')
const router = new Router()
const { postModel } = require('@/mongodb/model')

// 子路由1
router.get('/', async (ctx) => {
  let { sort, pageSize = 20, pageNo = 1 } = ctx.query.params
  pageSize = Number(pageSize)

  let resp = { code: 1, msg: 'ok', data: {} }
  try {
    const res = await postModel
      .find({})
      .sort(sort)
      .limit(pageSize)
      .skip((pageNo - 1) * pageSize)
      .exec()

    resp.data = { list: res }
  } catch (error) {
    resp = { code: 0, msg: error.message }
  }

  ctx.body = resp
})

module.exports = router.routes()
